package com.xls.springboot.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xls.springboot.dao.ChartData;
import com.xls.springboot.dao.ProjectRepository;
import com.xls.springboot.dao.StudentRepository;
import com.xls.springboot.dto.StudentProject;
import com.xls.springboot.entities.Project;
import com.xls.springboot.entities.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class HomeController {
    @Value("${version}")
    private String version;
    @Autowired
    ProjectRepository projectRepository;
    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/")
    public String displayHome(Model model) throws JsonProcessingException {

        model.addAttribute("versionNumber",version);

       List<Project> projects= projectRepository.findAll();
model.addAttribute("projects",projects);
List<ChartData> projectStatusData= projectRepository.getProjectStatus();
//转换projectStatusData JSON结构，方便再js脚本中使用
        ObjectMapper objectMapper=new ObjectMapper();
        String  jsonString = objectMapper.writeValueAsString(projectStatusData);
//        [{"未开始",1},{"进行中",1},{"已完成",1}]
        model.addAttribute("projectStatusData",jsonString);


        List<StudentProject> studentProjects=studentRepository.studentProjects();
model.addAttribute("studentProjects",studentProjects);
       return "main/home";
    }
}
